package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.lowcode.LowCodeConfigService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.lowcodeconfig.pojo.dto.LowCodeConfigListDTO;
import com.xbongbong.pro.lowcodeconfig.pojo.dto.LowCodeConfigSaveDTO;
import com.xbongbong.pro.lowcodeconfig.pojo.vo.LowCodeConfigListVO;
import com.xbongbong.pro.lowcodeconfig.pojo.vo.LowCodeConfigSaveVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/03/03 17:18
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/lowCode")
public class LowCodeConfigController {


    @Resource
    private LowCodeConfigService lowCodeConfigService;

    /**
     * 列表
     *
     * @param lowCodeConfigListDTO
     */
    @RequestMapping(value = "/formConfigList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid LowCodeConfigListDTO lowCodeConfigListDTO, BindingResult br) throws Exception {
        XbbResponse<LowCodeConfigListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LowCodeConfigListVO lowCodeConfigListVO = lowCodeConfigService.list(lowCodeConfigListDTO);
                response = new XbbResponse<>(lowCodeConfigListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 保存
     *
     * @param lowCodeConfigSaveDTO
     */
    @RequestMapping(value = "/formConfigSave", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String formConfigSave(@RequestBody @Valid LowCodeConfigSaveDTO lowCodeConfigSaveDTO, BindingResult br) throws Exception {
        XbbResponse<LowCodeConfigSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LowCodeConfigSaveVO lowCodeConfigListVO = lowCodeConfigService.save(lowCodeConfigSaveDTO);
                response = new XbbResponse<>(lowCodeConfigListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
